#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import logging

import webapp2
from google.appengine.ext import db
from models import Survey
import jinja2
import os
import urllib2

jinja_environment = jinja2.Environment( loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class MainHandler(webapp2.RequestHandler):
	def get(self):
		template = jinja_environment.get_template('/templates/index.html')
		self.response.out.write(template.render({}))

	def post(self):		
		genero = self.request.get("genero")
		ator_1 = self.request.get("ator_1")
		ator_2 = self.request.get("ator_2")
		semana_1 = int( self.request.get("semana_1") )
		semana_2 = int( self.request.get("semana_2") )
		semana_3 = int( self.request.get("semana_3") )
		semana_4 = int( self.request.get("semana_4") )
		semana_5 = int( self.request.get("semana_5") )

		logging.error(genero + " " + ator_1 + " " + ator_2 + " " + str(semana_1))

		data = Survey(	sv_genero=genero,sv_ator_1=ator_1,sv_ator_2=ator_2,sv_semana_1=semana_1,sv_semana_2=semana_2,sv_semana_3=semana_3,sv_semana_4=semana_4,sv_semana_5=semana_5)
		db.put(data)

		self.response.out.write( "{\"success\":\"teste\"}" )

class ChannelHandler(webapp2.RequestHandler):
	def get(self):
		template = jinja_environment.get_template('/templates/channel.html')
		self.response.out.write(template.render({}))

	def post(self):
		pass

class FacebookHandler(webapp2.RequestHandler):
	def get(self):
		code = self.request.get("code",None)
		if code is not None:
			self.response.set_cookie("access_token",code)

		template = jinja_environment.get_template('/templates/facebook_teste2.html')
		self.response.out.write(template.render({}))

	def post(self):
		pass	


app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/channel',ChannelHandler),
    ('/facebook',FacebookHandler)
], debug=True)
